package com.anrongtec.zcpt.util;

import android.content.Context;

import com.anrongtec.zcpt.base.DDApp;
import com.anrongtec.zcpt.interfaces.URLConstants;
import com.anrongtec.zcpt.interfaces.module.JsonDataRestult;
import com.anrongtec.zcpt.module.AppModel;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对象和json的相互转换
 * 
 * @author cxy
 * @param
 *
 */
public class GsonUtil {
	private static Map<String, String> baseInfo;
	private static Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
	private static Context context;
	// private static Map<String, Object> map;

	public static synchronized Gson getInstance() {
		if (gson == null) {
			gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
		}
		return gson;
	}

	/**
	 * 获取map格式公共参数
	 * 
	 * @return
	 */
	/*
	 * public static Map<String, Object> DDApp.dApp.getPublicParameter(){
	 * 
	 * if (map != null) { return map; }
	 * 
	 * map = new HashMap<String, Object>(); // map.put("busCode", getBusCode());
	 * map.put("appType", null); map.put("appVersion",
	 * Util.getVersionCode(context)); map.put("appOs", Util.getAppOS());
	 * map.put("deviceSN", Util.getDeviceSN(context)); map.put("deviceType",
	 * "Android"); map.put("mac", Util.getMac(context)); map.put("ip",
	 * Util.getIP(context)); map.put("longitude", MyLocation.getLongitude());
	 * map.put("latitude", MyLocation.getLatitude()); map.put("altitude",
	 * MyLocation.getAltitude()); //TODO 以下三项必须和登录时一致 map.put("imei",
	 * Util.getImei(context)); map.put("imsi", Util.getImsi(context));
	 * map.put("uuid", UUID.randomUUID().toString());
	 * 
	 * return map; }
	 */

	/**
	 * 解析json
	 * 
	 * @param json
	 * @param c
	 * @return
	 */
	/*
	 * public static <T> JsonDataRestult<T> decode(String json, Class<T> clazz){
	 * if (TextUtils.isEmpty(json)) { return null; } try { Type type = null; if
	 * (clazz.getClass() == User.class.getClass() ) { type = new
	 * TypeToken<JsonDataRestult<User>>() {}.getType(); } JsonDataRestult<T>
	 * fromJson = gson.fromJson(json, type); return fromJson;
	 * 
	 * } catch (JsonSyntaxException e) { e.printStackTrace(); } return null; }
	 */

	/**
	 * 解析json为type格式
	 * 
	 * @param json
	 * @param type
	 *            = new TypeToken<T>() {}.getType()
	 * @return
	 */
	public static <T> JsonDataRestult<T> decode(String json, Type type) {
		try {
			return gson.fromJson(json, type);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 用户登录
	 */
	public static String toLoginJson(String userName, String passwd, int loginType) {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.LOGIN);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);
		map.put("userName", userName);
		map.put("passwd", passwd);
		map.put("loginType", loginType);
		return gson.toJson(map);
	}

	/**
	 * 用户登录
	 */
	public static String toLoginJson(String userName, String passwd,String phonenumber,String scrertID,int loginType) {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.LOGIN);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);
		map.put("userName", userName);
		map.put("passwd", passwd);
		map.put("loginType", loginType);
		//三码绑定添加的两个参数
		map.put("phonenumber", phonenumber);
		map.put("secretId",scrertID);
		return gson.toJson(map);
	}
	/**
	 * 用户登录
	 */
	public static String toLoginJson(String idno) {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.LOGIN);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);
		map.put("sfzh", idno);
		map.put("loginType", 3);
		return gson.toJson(map);
	}
	/**
	 * 用户登出
	 */
	public static String toLogoutJson() {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.LOGOUT);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);

		return gson.toJson(map);
	}

	/**
	 * 轮播图
	 */
	public static String toBannerJson() {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.BANNER);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);

		return gson.toJson(map);
	}

	/**
	 * 查询app
	 * 
	 * @param wd
	 *            关键字
	 * @param sort
	 *            排序方式(按照最新查询1、按照下载量查询2、按照好评量查询3 )
	 * @param type
	 *            分类查询方式：警种分类(PoliceType)、地区分类(AreaType)、功能分类(BusType)
	 * @param typeVal
	 *            分类查询的值
	 * @param page
	 *            当前查询页
	 * @return
	 */
	public static String toQueryJson(String wd, String sort, String type, String typeVal, int page) {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.QUERY);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);
		map.put("wd", wd);
		map.put("sort", sort);
		map.put("type", type);
		map.put("typeVal", typeVal);
		map.put("page", page);
		return gson.toJson(map);
	}

	public static String toQueryAppJson(String wd, List<com.anrongtec.zcpt.module.Type> type, int page) {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.QUERY);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);
		map.put("wd", wd);
		for (com.anrongtec.zcpt.module.Type t : type) {
			map.put(t.getParentValue(), t.getValue());
		}
		map.put("page", page);
		return gson.toJson(map);
	}

	/**
	 * 分类查询(查询的是有哪些分类项目)
	 */
	public static String toTypeQueryJson() {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.TYPE_QUERY);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);

		return gson.toJson(map);
	}

	/**
	 * 应用列表查询接口
	 */
	public static String toTypeQueryApps(List<String> list) {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.SEARCH_ALL_APP);
//		LogUtil.e("包名==" + list.get(0));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);
		map.put("pkgs",list);
		map.put("type","N");
		return gson.toJson(map);
	}


	/**
	 * 下载
	 */
	public static String toDownloadJson(String fileId) {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.DOWNLOAD);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);
		map.put("fileId", fileId);

		return gson.toJson(map);
	}

	/**
	 * app更新检测
	 */
	public static String toAppUpgradeJson(List<AppModel> apps) {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.APP_UPGRADE);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < apps.size(); i++) {
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("pkg", apps.get(i).getAppPackName());
			m.put("versionCode", apps.get(i).getLocalVersionCode());
			list.add(m);
		}
		map.put("apps", list);

		return gson.toJson(map);
	}

	/**
	 * 应用商店升级检查
	 */
	public static String toUpgradeJson(int versionCode) {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.UPGRADE);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);
		map.put("versionCode", versionCode);

		return gson.toJson(map);
	}

	/**
	 * 意见反馈
	 */
	public static String toFeedbackJson(int versionCode, String feedback) {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.FEEDBACK);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);
		map.put("versionCode", versionCode);
		map.put("feedback", feedback);

		return gson.toJson(map);
	}

	/**
	 * 版本信息
	 */
	public static String toVersionInfoJson(String versionCode) {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.VERSION_INFO);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);
		map.put("versionCode", versionCode);

		return gson.toJson(map);
	}

	/**
	 * App安装卸载日志上报
	 */
	public static String toUploadJson(String type, String pkg, int versionCode) {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.DATA_UPLOAD);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);
		map.put("type", type);
		map.put("pkg", pkg);
		map.put("versionCode", versionCode);

		return gson.toJson(map);
	}

	/**
	 * 评论打分
	 */
	public static String toCommentJson(String pkg, int versionCode, float score, String comment) {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.COMMENT);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);
		map.put("pkg", pkg);
		map.put("versionCode", versionCode);
		map.put("score", score);
		map.put("comment", comment);

		return gson.toJson(map);
	}

	/**
	 * 评论查询
	 */
	public static String toQueryCommentJson(String pkg, int versionCode, int page) {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.QUERY_COMMENT);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);
		map.put("pkg", pkg);
		map.put("versionCode", versionCode);
		map.put("page", page);

		return gson.toJson(map);
	}

	/**
	 * 按版本号和包名查询app
	 */
	public static String toSearchApp(String pkg, int versionCode) {
		baseInfo = DDApp.dApp.getBaseInfo();
		baseInfo.put(URLConstants.BUSCODE, URLConstants.BusCode.SEARCH_APP);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("baseInfo", baseInfo);
		map.put("pkg", pkg);
		map.put("versionCode", versionCode);

		return gson.toJson(map);
	}
	/*转化json*/
	public static String toJson(Map<String,String>map) {

		return gson.toJson(map);
	}
}
